/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.editor.field;

import java.io.Serializable;

import com.daikit.daikit4gxt.client.DkBaseMain;
import com.daikit.daikit4gxt.client.action.DkBaseAsyncCallback;
import com.daikit.daikit4gxt.client.action.DkBasePagingLoader;
import com.daikit.daikit4gxt.client.action.DkPagingToolbar;
import com.daikit.daikit4gxt.client.action.DkPagingRpcCallerWithInput;
import com.daikit.daikit4gxt.client.action.DkQueryPagingLoadConfig;
import com.daikit.daikit4gxt.client.labelprovider.DkAsSimpleStringLabelProvider;
import com.sencha.gxt.data.shared.LabelProvider;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.widget.core.client.toolbar.PagingToolBar;


/**
 * A cell for {@link DkDynamicAsyncPagingCombobox}
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 * @param <B>
 */
public class DkDynamicAsyncPagingComboboxCell<B extends Serializable> extends DkComboboxCell<B>
{

	private DkPagingToolbar<B, PagingLoadConfig, PagingLoadResult<B>> pagingToolbar;
	private final DkBasePagingLoader<B, PagingLoadConfig, PagingLoadResult<B>> basePagingLoader;

	/**
	 * Wrapper to {@link #DkDynamicAsyncPagingComboboxCell(ListStore, LabelProvider, DkPagingRpcCallerWithInput)} with a
	 * {@link DkAsSimpleStringLabelProvider}
	 * 
	 * @param store
	 * @param rpcCaller
	 */
	public DkDynamicAsyncPagingComboboxCell(final ListStore<B> store, final DkPagingRpcCallerWithInput<B> rpcCaller)
	{
		this(store, new DkAsSimpleStringLabelProvider<B>(), rpcCaller);
	}

	/**
	 * Constructor
	 * 
	 * @param store
	 * @param labelProvider
	 * @param rpcCaller
	 */
	public DkDynamicAsyncPagingComboboxCell(final ListStore<B> store, final LabelProvider<B> labelProvider,
			final DkPagingRpcCallerWithInput<B> rpcCaller)
	{
		super(store, labelProvider);
		basePagingLoader = initializePagingLoader(getStore(), rpcCaller);
	}

	/**
	 * Constructor
	 * 
	 * @param store
	 * @param labelProvider
	 * @param rpcCaller
	 * @param choicesTemplate
	 * @param cssBundle
	 */
	public DkDynamicAsyncPagingComboboxCell(final ListStore<B> store, final LabelProvider<B> labelProvider,
			final DkPagingRpcCallerWithInput<B> rpcCaller, final MyDynamicComboboxXTemplate<B> choicesTemplate,
			final MyDynamicComboboxCssBundle<?> cssBundle)
	{
		super(store, labelProvider, choicesTemplate, cssBundle);
		basePagingLoader = initializePagingLoader(getStore(), rpcCaller);
	}

	/**
	 * Paging Loader initialization method
	 * 
	 * @param store
	 * @param rpcCaller
	 * @return the {@link DkBasePagingLoader}
	 */
	protected DkBasePagingLoader<B, PagingLoadConfig, PagingLoadResult<B>> initializePagingLoader(final ListStore<B> store,
			final DkPagingRpcCallerWithInput<B> rpcCaller)
	{
		final DkBasePagingLoader<B, PagingLoadConfig, PagingLoadResult<B>> basePagingLoader = new DkBasePagingLoader<B, PagingLoadConfig, PagingLoadResult<B>>(
				store, "Loading...", DkBaseMain.SCREEN_PAGING_SIZE, false)
		{
			@Override
			protected void doLoad(final PagingLoadConfig pagingLoadConfig, final DkBaseAsyncCallback<PagingLoadResult<B>> callback)
			{
				rpcCaller.callLoadRpc(((DkQueryPagingLoadConfig) pagingLoadConfig).getQuery(), pagingLoadConfig, callback);
			}
		};
		setLoader(basePagingLoader);
		setMinChars(0);
		setQueryDelay(DkBaseMain.config().getAsyncComboboxQueryDelayMilliseconds());
		this.pagingToolbar = basePagingLoader.getPagingToolbar();
		setPageSize(basePagingLoader.getPagingToolbar().getPageSize());
		return basePagingLoader;
	}

	/**
	 * To use our own paging toolBar
	 */
	@Override
	protected final PagingToolBar createPagingToolBar(final int pageSize)
	{
		return pagingToolbar == null ? super.createPagingToolBar(pageSize) : pagingToolbar;
	}

	/**
	 * @return the pagingToolbar
	 */
	public DkPagingToolbar<B, PagingLoadConfig, PagingLoadResult<B>> getPagingToolbar()
	{
		return pagingToolbar;
	}

	/**
	 * @return the basePagingLoader
	 */
	public DkBasePagingLoader<B, PagingLoadConfig, PagingLoadResult<B>> getBasePagingLoader()
	{
		return basePagingLoader;
	}
}
